package com.sr.examination.user.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import com.sr.examination.R;
import com.sr.examination.exam.grade.ExamGradeActivity;
import com.sr.examination.net.Net;
import com.sr.examination.user.Account;
import com.tl.commonlibrary.network.ErrorResponse;
import com.tl.commonlibrary.network.RequestListener;
import com.tl.commonlibrary.network.bean.base.BaseBean;
import com.tl.commonlibrary.tool.CountDownTimer;
import com.tl.commonlibrary.tool.UIUtils;
import com.tl.commonlibrary.ui.ActivityStack;
import com.tl.commonlibrary.ui.BaseFragmentActivity;
import com.tl.commonlibrary.ui.beans.User;

import retrofit2.Call;

/**
 * Created by JongSung on 18/2/25.
 */

public class RegisterPasswordActivity extends BaseFragmentActivity {
    public static void start(Context context, String phone, String sms, String verifyCode, boolean resetPassword) {
        Intent starter = new Intent(context, RegisterPasswordActivity.class);
        starter.putExtra("phone", phone);
        starter.putExtra("resetPassword", resetPassword);
        starter.putExtra("sms", sms);
        starter.putExtra("verifyCode", verifyCode);
        context.startActivity(starter);
    }

    private EditText passwordEText, rePasswordEText;
    private String phone, verifyCode, sms;
    private boolean resetPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_register_password);
        if (getIntent() != null) {
            resetPassword = getIntent().getBooleanExtra("resetPassword", false);
        }
        if (resetPassword) {
            setTitle(getString(R.string.reset_password));
        } else {
            setTitle(getString(R.string.register_for_password));
        }
        phone = getIntent().getStringExtra("phone");
        verifyCode = getIntent().getStringExtra("verifyCode");
        sms = getIntent().getStringExtra("sms");
        passwordEText = findViewById(R.id.passwordEText);
        rePasswordEText = findViewById(R.id.rePasswordEText);
        findViewById(R.id.enterBtn).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.enterBtn:
                next();
                break;
        }

    }

    private void next() {
        String password = passwordEText.getText().toString().trim();
        final String rePassword = rePasswordEText.getText().toString().trim();
        if (TextUtils.isEmpty(password)) {
            UIUtils.showToast(getString(R.string.hint_password_empty));
            return;
        }
        if (password.length() < 6 || password.length() > 20) {
            UIUtils.showToast(getString(R.string.hint_password_invalid));
            return;
        }
        if (TextUtils.isEmpty(rePassword)) {
            UIUtils.showToast(getString(R.string.hint_re_password_empty));
            return;
        }
        if (!password.equals(rePassword)) {
            UIUtils.showToast(getString(R.string.hint_password_inconsistent));
            return;
        }
        if (resetPassword) {
            showProgressDialog(getString(R.string.progress_reset_password));
            Net.resetPassword(phone, verifyCode, sms, password, new RequestListener<BaseBean>() {
                @Override
                public void onSuccess(Call<BaseBean> call, BaseBean response) {
                    UIUtils.showToast(getString(R.string.hint_reset_password_success));
                    dismissAll();
                    ActivityStack.get().popByClass(RegisterActivity.class);
                    finish();
                }

                @Override
                public void onFailed(Call<BaseBean> call, ErrorResponse errorResponse) {
                    dismissAll();
                }
            });
        } else {
            showProgressDialog(getString(R.string.progress_registering));
            Net.register(phone, verifyCode, sms, password, new RequestListener<BaseBean<User>>() {
                @Override
                public void onSuccess(Call<BaseBean<User>> call, BaseBean<User> response) {
                    if (response.isSuccessful() && response.data != null) {
                        Account.setUser(response.data);
                        dismissAll();
                        ExamGradeActivity.start(context, ExamGradeActivity.TYPE_CREATE);
                        ActivityStack.get().popByClass(RegisterActivity.class);
                        ActivityStack.get().popByClass(LoginActivity.class);
                        finish();
                    }
                }

                @Override
                public void onFailed(Call<BaseBean<User>> call, ErrorResponse errorResponse) {
                    dismissAll();
                }
            });
        }
    }

    @Override
    public void onFragmentTrigger(int tag, Bundle bundle) {

    }
}
